<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="java.util.List, book.BookBean" %>
<%@page import="java.util.*, common.selectMenu" %>
<%
 	session.setAttribute("stage", "2");
// リクエスト文字列のエンコードを設定
	request.setCharacterEncoding("UTF-8");
%>

<jsp:include page="/share/layout.jsp">
    <jsp:param name="title" value="書籍情報変更"/>
    <jsp:param name="css" value="book.css"/>
    <jsp:param name="content">
        <jsp:attribute name="value">
<!-- contents start -->

<div id="bookEdit">

<%@ page import="java.sql.*"%>

<%
	BookBean bean = (BookBean)request.getAttribute("bean");

	String publisher = (String)request.getAttribute("publisher");

	boolean g_back = false;
	String genre_back = (String)request.getAttribute("genre_back");
	if(genre_back == null || genre_back.equals("")){

	}else{
		g_back = true;
	}

	boolean p_back = false;
	String publisher_back = (String)request.getAttribute("publisher_back");
	if(publisher_back == null || publisher_back.equals("")){

	}else{
		p_back = true;
	}

	boolean genre_f = false;
	String genre_flag = (String)request.getAttribute("genre_f");
	if(genre_flag == null || genre_flag.equals("")){

	}else if(genre_flag.equals("genre_f")){
		genre_f = true;
	}
%>

<%
//  公開・非公開ボタンの表示
	Boolean open_flag = bean.isOpenFlag();
 	String open_flag_1 = "";
 	String open_flag_0 = "";
	if(open_flag){
 		open_flag_1 = "checked=\"checked\"";
 	}else{
 		open_flag_0 = "checked=\"checked\"";
 	}
%>


<%-- エラーメッセージが設定されていればフィールドの枠を赤くする処理 --%>
<%
	String error_title = (String)request.getAttribute("error_title");
	String error_writer = (String)request.getAttribute("error_writer");
	String error_publisher = (String)request.getAttribute("error_publisher");
	String error_genre_id = (String)request.getAttribute("error_genre_id");
	String error_isbn = (String)request.getAttribute("error_isbn");
	String error_publication_date = (String)request.getAttribute("error_publication_date");
	String error_content = (String)request.getAttribute("error_content");

	// 取得できなければ空白を設定
	if(error_title == null || error_title.equals("")){
		error_title = "";
	}
	if(error_writer == null || error_writer.equals("")){
		error_writer = "";
	}
	if(error_publisher == null || error_publisher.equals("")){
		error_publisher = "";
	}
	if(error_genre_id == null || error_genre_id.equals("")){
		error_genre_id = "";
	}
	if(error_isbn == null || error_isbn.equals("")){
		error_isbn = "";
	}
	if(error_publication_date == null || error_publication_date.equals("")){
		error_publication_date = "";
	}
	if(error_content == null || error_content.equals("")){
		error_content = "";
	}

	String error_class_title = null;
	String error_class_writer = null;
	String error_class_publisher = null;
	String error_class_genre_id = null;
	String error_class_isbn = null;
	String error_class_publication_date = null;
	String error_class_content = null;

	if(error_title.equals("error")){
		error_class_title = "class=\"red_frame\"";
	}else{
		error_class_title = "";
	}
	if(error_writer.equals("error")){
		error_class_writer = "class=\"red_frame\"";
	}else{
		error_class_writer = "";
	}
	if(error_publisher.equals("error")){
		error_class_publisher = "class=\"red_frame\"";
	}else{
		error_class_publisher = "";
	}
	if(error_genre_id.equals("error")){
		error_class_genre_id = "class=\"red_frame\"";
	}else{
		error_class_genre_id = "";
	}
	if(error_isbn.equals("error")){
		error_class_isbn = "class=\"red_frame\"";
	}else{
		error_class_isbn = "";
	}
	if(error_publication_date.equals("error")){
		error_class_publication_date = "class=\"red_frame\"";
	}else{
		error_class_publication_date = "";
	}
	if(error_content.equals("error")){
		error_class_content = "class=\"red_frame\"";
	}else{
		error_class_content = "";
	}
 %>

	<div class="book_img">
		<p>
		<img src="http://192.168.3.190:8080/Books/book_img/<%= bean.getPicture() %>.jpg" width="160px" height="160px" />
		</p>
	</div>

	<form action="/Books/book/EditServlet" method="post">

		<!-- 書籍画像のデータを渡す -->
		<input type="hidden" name="picture" value="<%= bean.getPicture() %>" />

	<%-- エラーメッセージが設定されていれば表示 --%>
	<p class="error_message">
		<%
			@SuppressWarnings("unchecked")
			ArrayList<String> error = (ArrayList<String>)request.getAttribute("error_message");
			if(error != null && !error.equals("")){

				for( int i=0 ; i<error.size() ; i++ ){
		%>
					<%=error.get( i ) %><br />
		<%
				}
			}
		%>
	</p>

		<p class="msg">変更内容を入力して下さい</p>

	  <table class="form_table">

			<tr>
				<th>書籍ID</th>
				<td><%=bean.getId() %><input type="hidden" name="id" value="<%= bean.getId() %>" /></td>
			</tr>
			<tr>
				<th>タイトル</th>
				<td><textarea name="title" cols="40" rows="2" <%= error_class_title %> ><%=bean.getTitle() %></textarea></td>
			</tr>
			<tr>
				<th>著者名</th>
				<td><input type="text" name="writer" size="40" value="<%=bean.getWriter() %>" <%= error_class_writer %>  maxlength="100" /></td>
			</tr>
			<tr>
				<th>出版者</th>
				<td>
				<%			System.out.println(p_back);
				if(p_back){%>
					<input type="text" name="publisher" size="40" value="<%=publisher %>"  <%= error_class_publisher %>  maxlength="50" />
				<%}else{%>
					<input type="text" name="publisher" size="40" value="<%=bean.getPublisher() %>" maxlength="50" />
					<input type="hidden" name="publisher_id" value="<%=bean.getPublisherId() %>" />
				<%}%>
				</td>
			</tr>
			<tr>
				<th>ジャンル</th>
				<td>
				<input type="hidden" name="genre_id" value="<%=bean.getGenreId() %>" />
				<select name="genre" style="width:100px" <%= error_class_genre_id %>>
					<option value="">選択</option>
					<%
					selectMenu select = new selectMenu();
					Map<Integer,String> map = select.genreShow();

					if(g_back){
						if(genre_f){
							map = select.genreShow();
							for(Map.Entry<Integer, String> me : map.entrySet()) {
								if(me.getKey() == bean.getGenreId()){%>
								<option value="<%= me.getKey() %>" selected="selected"><%= me.getValue() %></option>
							<%}else{%>
								<option value="<%= me.getKey() %>"><%= me.getValue() %></option>
							<%}
							}
						}else{
							map = select.genreShow();
							for(Map.Entry<Integer, String> me : map.entrySet()) {
						%>
 							<option value="<%= me.getKey() %>"><%= me.getValue() %></option>

						<% }
						}
					}else{
						map = select.genreShow();
						for(Map.Entry<Integer, String> me : map.entrySet()) {
							if(me.getKey() == bean.getGenreId()){%>
							<option value="<%= me.getKey() %>" selected="selected"><%= me.getValue() %></option>
						<%}else{%>
							<option value="<%= me.getKey() %>"><%= me.getValue() %></option>
						<%}
						}
					}%>
				</select>
				</td>
			</tr>
			<tr>
				<th>ISBNコード</th>
				<td><input type="text" name="isbn" size="40" maxlength="20" value="<%= bean.getIsbn() %>" <%= error_class_isbn %>  /> <span class="small">(※半角)</span></td>
			</tr>
			<tr>
				<th>発行年月日</th>
				<td>
				<input type="text" name="publication_date" size="12" value="<%= bean.getPublicationDate() %>" <%= error_class_publication_date %>  maxlength="10" /> <span class="small">記入例：2013-10-23 (※半角)</span>
				</td>
			</tr>
			<tr>
				<th>書籍公開</th>
				<td>
					<input type="radio" name="open_flag" value="１" <%= open_flag_1 %> /> 公開　　
					<input type="radio" name="open_flag" value="0" <%= open_flag_0 %> /> 非公開
				</td>
			</tr>
			<tr>
				<th>内容</th>
				<td><textarea name="content" cols="40" rows="5" <%= error_class_content %> ><%= bean.getContent() %></textarea></td>
			</tr>

		</table>

        <div class="buttons">
  		  <input type="button" value="戻る" class="button" style="cursor:pointer" onClick="history.back(); return false;" title="『書籍検索結果』へ">
  		  <input type="submit" value="確認" class="button" title="『書籍情報変更確認』へ" style="cursor:pointer">
        </div>
		</form>

</div>

<!-- contents end -->
        </jsp:attribute>
    </jsp:param>
</jsp:include>